package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/12 15:29
 * https://leetcode-cn.com/problems/house-robber-ii/
 */
public class Title213 {

    /**
     * dp[i]表示偷0-i号房子获得的最高金额
     * 递推公式：dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i])
     * @param nums
     * @return
     */
    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        } else if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int max;
        int[] dp = new int[nums.length];
        //包括首元素
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < nums.length - 1; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
        }
        max = dp[nums.length - 2];
        //包括尾元素
        dp[1] = nums[1];
        dp[2] = Math.max(nums[1], nums[2]);
        for (int i = 3; i < nums.length; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
        }
        return max > dp[nums.length - 1] ? max : dp[nums.length - 1];
    }

    public static void main(String[] args) {

    }
}
